﻿using D.B.Data.API.Applications.Command.RoleMenusCommand;
using D.B.Data.Domain;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;

namespace D.B.Data.API.Applications.CommandHandler.RoleMenusCommandHandler
{
    public class RoleIdGetMenusListCommandHandler : IRequestHandler<RoleIdGetMenusListCommand, APIResult<List<long>>>
    {
        private readonly IBaseRepository<RoleMenusModel> baseRepository;

        public RoleIdGetMenusListCommandHandler(IBaseRepository<RoleMenusModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public Task<APIResult<List<long>>> Handle(RoleIdGetMenusListCommand request, CancellationToken cancellationToken)
        {

            APIResult<List<long>> aPIResult = new APIResult<List<long>>();
           var res= baseRepository.GetAll().Where(s=>s.RID==request.RId).Select(s=>s.MID).ToList();
            if(res.Any())
            {
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "显示成功";
                aPIResult.Data = res;
            }
            else
            {
                aPIResult.Code = EnumCode.File;
                aPIResult.Data = res;
                aPIResult.Msg = "显示失败";
            }
            return  Task.FromResult(aPIResult);
           

        }
    }
}
